/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package doan2database;

/**
 *
 * @author DongVan
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
public class Datapro {
    private  java.sql.Statement statement = null;
	private String url = "";
	private Connection conn = null;
	//protected java.sql.Statement statement = null;
	public Datapro(){
		url = "jdbc:sqlserver://DONGVAN-PC\\SQLEXPRESS;user=user1;password=12345678;database=HeThongBanVeTauQuaMang";
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			conn = DriverManager.getConnection(url);
			statement = conn.createStatement();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	//Select databse
	public ResultSet Selected(String sqlQuery){
		
		ResultSet rs = null;
		try {
			rs = statement.executeQuery(sqlQuery);
			return rs;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return rs;
	}
	
	//Insert database
	public boolean Insert(String sqlQuery){
		try {
			statement.executeUpdate(sqlQuery);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	//Update database
	
	public boolean Update(String sqlQuery){
		try {
			statement.executeUpdate(sqlQuery);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean Delete(String sqlQuery){
		try {
			statement.executeUpdate(sqlQuery);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	//Delete database
	public void disconnect(){
		try {
			statement.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
